﻿using System;
using System.Data;
using System.Data.OleDb;
using System.IO;
using MySql.Data.MySqlClient;
using LdExpress;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace LdExpress
{
    public class PriceAreas
    {
        public long ID = -1;
        public string DC20 = "";
        public string DC40 = "";
        public string HC40 = "";
        public string RF20 = "";
        public string RF40 = "";
        public string Validity = "";
        public string Note = "";
        public long CarrID = 0;
        public long PolID = 0;
        public long PodID = 0;
        public long ServiceAreaID = 0;
        public string CarrName = "";
        public string PolName = "";
        public string PodName = "";
        public string ServiceAreaName = "";
        public DateTime LastUpdate = DateTime.Now;
        public int Sort = 0;
        public long UserID = 0;

        public PriceAreas()
        {
        }

        public PriceAreas(long lID)
        {
            this.ID = lID;
        }

        public void Save(MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";
            string strTableName = "tbl_PriceAreas";

            if (ID <= 0) // new object, we insert new record to database
            {
                //ID = long.Parse(DBModule.GetNewID(typeof(Bill), "tbl_bill" , "ID", "ID", cn, trans));
                strSQL = "Insert into " + strTableName + " (" +
                             "`DC20`" +
                             ",`DC40`" +
                             ",`HC40`" +
                             ",`RF20`" +
                             ",`RF40`" +
                             ",`Validity`" +
                             ",`Note`" +
                             ",`ServiceAreaID`" +
                             ",`CarrID`" +
                             ",`PolID`" +
                             ",`PodID`" +
                             ",`Sort`" +
                             ",`UserID`" +
                             ",`LastUpdate`" +
                             ") " +
                         " values(" +
                             "'" + DBModule.RefineString(DC20) + "'" +
                             "," + "'" + DBModule.RefineString(DC40) + "'" +
                             "," + "'" + DBModule.RefineString(HC40) + "'" +
                             "," + "'" + DBModule.RefineString(RF20) + "'" +
                             "," + "'" + DBModule.RefineString(RF40) + "'" +
                             "," + "'" + DBModule.RefineString(Validity) + "'" +
                             "," + "'" + DBModule.RefineString(Note) + "'" +
                             "," + ServiceAreaID.ToString() +
                             "," + CarrID.ToString() +
                             "," + PolID.ToString() +
                             "," + PodID.ToString() +
                             "," + Sort.ToString() +
                             "," + UserID.ToString() +
                             "," + DBModule.RefineDatetime(LastUpdate) +
                             ")";
                // run SQL statement
                //throw new Exception(strSQL);
                DBModule.ExecuteNonQuery(strSQL, cn, trans);
                UserObjectsLog.SaveObjectActionLog("PriceAreas", "add", cn, trans);
            }
            else // edit object, we update old record in database
            {
                // build SQL statement
                strSQL = "Update " + strTableName + " set " +
                            "DC20='" + DBModule.RefineString(DC20) + "'" +
                             ",DC40='" + DBModule.RefineString(DC40) + "'" +
                             ",HC40='" + DBModule.RefineString(HC40) + "'" +
                             ",RF20='" + DBModule.RefineString(RF20) + "'" +
                             ",RF40='" + DBModule.RefineString(RF40) + "'" +
                             ",Validity='" + DBModule.RefineString(Validity) + "'" +
                             ",Note='" + DBModule.RefineString(Note) + "'" +
                             ",ServiceAreaID = " + ServiceAreaID.ToString() +
                             ",CarrID = " + CarrID.ToString() +
                             ",PolID = " + PolID.ToString() +
                             ",PodID = " + PodID.ToString() +
                             ",Sort = " + Sort.ToString() +
                             ",UserID = " + UserID.ToString() +
                             ",LastUpdate=" + DBModule.RefineDatetime(LastUpdate) + "" +
                    " where ID=" + ID.ToString();
                // run SQL statement
                DBModule.ExecuteNonQuery(strSQL, cn, trans);
                UserObjectsLog.SaveObjectActionLog("PriceAreas", "edit", cn, trans);
            }
        }
        public void Delete(MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "DELETE FROM tbl_PriceAreas WHERE ID = " + this.ID.ToString();
            DBModule.ExecuteNonQuery(strSQL, cn, trans);
            UserObjectsLog.SaveObjectActionLog("PriceAreas", "delete", cn, trans);
        }
        public void Load(MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "SELECT tbl_PriceAreas.*,POL.Name as POLName,POD.Name as PODName " +
            ",SerAreas.Name as ServiceAreaName,SerAreas.Sort as ServiceSort,CARR.Name as CarrName " +
            " FROM tbl_PriceAreas " +
            " LEFT JOIN tbl_ports POL ON POL.ID=tbl_PriceAreas.PolID " +
            " LEFT JOIN tbl_ports POD ON POD.ID=tbl_PriceAreas.PodId " +
            " LEFT JOIN tbl_ServiceAreas SerAreas ON SerAreas.ID=tbl_PriceAreas.ServiceAreaID " +
            " LEFT JOIN tbl_carrs CARR ON CARR.ID=tbl_PriceAreas.CarrID " +
            " WHERE tbl_PriceAreas.ID = " + ID.ToString() + " limit 0,1";

            DataSet ds = DBModule.ExecuteQuery(strSQL, cn, trans);
            //Fill data into this object
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                if (!dr.IsNull("ID")) ID = long.Parse(dr["ID"].ToString());
                if (!dr.IsNull("DC20")) DC20 = dr["DC20"].ToString();
                if (!dr.IsNull("DC40")) DC40 = dr["DC40"].ToString();
                if (!dr.IsNull("HC40")) HC40 = dr["HC40"].ToString();
                if (!dr.IsNull("RF20")) RF20 = dr["RF20"].ToString();
                if (!dr.IsNull("RF40")) RF40 = dr["RF40"].ToString();
                if (!dr.IsNull("Validity")) Validity = dr["Validity"].ToString();
                if (!dr.IsNull("Note")) Note = dr["Note"].ToString();
                if (!dr.IsNull("ServiceAreaID")) ServiceAreaID = long.Parse(dr["ServiceAreaID"].ToString());
                if (!dr.IsNull("CarrID")) CarrID = long.Parse(dr["CarrID"].ToString());
                if (!dr.IsNull("PolID")) PolID = long.Parse(dr["PolID"].ToString());
                if (!dr.IsNull("PodID")) PodID = long.Parse(dr["PodID"].ToString());
                if (!dr.IsNull("Sort")) Sort = int.Parse(dr["Sort"].ToString());
                if (!dr.IsNull("UserID")) UserID = int.Parse(dr["UserID"].ToString());
                if (!dr.IsNull("PolName")) PolName = dr["PolName"].ToString();
                if (!dr.IsNull("PodName")) PodName = dr["PodName"].ToString();
                if (!dr.IsNull("CarrName")) CarrName = dr["CarrName"].ToString();
                if (!dr.IsNull("ServiceAreaName")) ServiceAreaName = dr["ServiceAreaName"].ToString();
                if (!dr.IsNull("LastUpdate")) LastUpdate = DateTime.Parse(dr["LastUpdate"].ToString());
            }
        }
        public static void GetList(string condition, string groupAreas, string OrderBy, int NumOfItemsPerPage, int PageNo, out int NumberOfPages, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            NumberOfPages = 0;
            string strSQL = "";
            ds = null;
            // build SQL statement
            strSQL = "SELECT tbl_PriceAreas.*,POL.Name as POLName,POD.Name as PODName" +
            ",SerAreas.Name as ServiceAreaName,SerAreas.Sort as ServiceSort" +
            ",SerAreas.GroupAreas as ServiceAreaGroup,CARR.Name as CarrName " +
           " FROM tbl_PriceAreas " +
           " LEFT JOIN tbl_ports POL ON POL.ID=tbl_PriceAreas.PolID " +
           " LEFT JOIN tbl_ports POD ON POD.ID=tbl_PriceAreas.PodId " +
           " LEFT JOIN tbl_ServiceAreas SerAreas ON SerAreas.ID=tbl_PriceAreas.ServiceAreaID " +
           " LEFT JOIN tbl_carrs CARR ON CARR.ID=tbl_PriceAreas.CarrID " +
           " WHERE 1=1";
            if (condition.Length > 0)
            {
                strSQL += condition;
            }
            if (groupAreas.Length > 0)
            {
                strSQL += " HAVING ServiceAreaGroup='" + DBModule.RefineString(groupAreas) + "'";
            }
            if ((OrderBy != null) && (OrderBy != ""))
                strSQL = strSQL + " Order By " + DBModule.RefineString(OrderBy);
            else
                strSQL += " Order By ServiceSort ASC";
            //GV.Response.Write(strSQL);
            // run SQL statement
            if (NumOfItemsPerPage > 0)
            {
                ds = DBModule.ExecuteQuery(strSQL, NumOfItemsPerPage, PageNo, out NumberOfPages, cn, trans);
            }
            else
                ds = DBModule.ExecuteQuery(strSQL, cn, trans);

        }
        public void DoSearch(string strKeywords, string OrderBy, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";
            ds = null;
            // build SQL statement
            strSQL = "Select `ID` from tbl_PriceAreas where 1=1 ";

            if ((OrderBy != null) && (OrderBy != ""))
                strSQL = strSQL + " Order By " + DBModule.RefineString(OrderBy);
            // run SQL statement
            ds = DBModule.ExecuteQuery(strSQL, cn, trans);
        }

        public static void InitializeData(long carrID, long polID, long podID, MySqlConnection cn, MySqlTransaction trans)
        {
            DataSet ds = null;
            ServiceAreas.DoSearch("", "", out ds, cn, trans);
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                long serviceAreaID = long.Parse(dr["ID"].ToString());
                PriceAreas obj = new PriceAreas();
                obj.CarrID = carrID;
                obj.PolID = polID;
                obj.PodID = podID;
                obj.ServiceAreaID = serviceAreaID;
                obj.LastUpdate = DateTime.MinValue;
                string lID = DBModule.ExecuteQueryForOneResult("Select ID from tbl_PriceAreas where CarrID=" + carrID +
                    " AND PolID=" + polID + " AND PodID=" + podID + " AND ServiceAreaID=" + serviceAreaID, cn, trans);
                if (lID == null)
                    obj.Save(cn, trans);
            }
        }
        public static void InitializeDataForOneArea(long carrID, long polID, long podID, long serviceAreaID, MySqlConnection cn, MySqlTransaction trans)
        {
            PriceAreas obj = new PriceAreas();
            obj.CarrID = carrID;
            obj.PolID = polID;
            obj.PodID = podID;
            obj.ServiceAreaID = serviceAreaID;
            obj.LastUpdate = DateTime.MinValue;
            string lID = DBModule.ExecuteQueryForOneResult("Select ID from tbl_PriceAreas where CarrID=" + carrID +
                " AND PolID=" + polID + " AND PodID=" + podID + " AND ServiceAreaID=" + serviceAreaID, cn, trans);
            if (lID == null)
                obj.Save(cn, trans);
        }
    }
}
